package com.hainatong.shiphelper;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hainatong.shiphelper.databinding.ActivityPictureCheckBinding;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.hainatong.mvvmbase.base.NoViewModel;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class PictureCheckActivity extends BaseMvvmActivity<ActivityPictureCheckBinding, NoViewModel>
{

    public static void start(Context context,String imgUrl)
    {
        Intent start = new Intent(context ,PictureCheckActivity.class);
        start.putExtra(PIC_URL,imgUrl);
        context.startActivity( start );
    }

    public static final String PIC_URL = "picUrl";

    private String imgUrl;

    private Bitmap mBitmap;

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_picture_check;
    }

    @Override
    protected int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initView()
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initData()
    {
        if (getIntent().getStringExtra(PIC_URL) != null)
        {
            imgUrl = getIntent().getStringExtra(PIC_URL);
            Glide.with(this).asBitmap().load(imgUrl).into(new SimpleTarget<Bitmap>()
            {
                @Override
                public void onResourceReady(@NonNull Bitmap resource,
                                            @Nullable Transition<? super Bitmap> transition)
                {
                    Log.d(TAG, "resource : " + resource);
                    mBitmap = resource;
                    viewDataBinding.ivMyImg.setImage(mBitmap);
                }
            });
        }
    }

    @Override
    protected void onRetryBtnClick()
    {
    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        new Thread(() -> Glide.get(PictureCheckActivity.this).clearDiskCache()).start();
        Glide.get(PictureCheckActivity.this).clearMemory();

    }
}
